// $codepro.audit.disable
package edu.gatech.oad.cyberdoc.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.json.JSONObject;

import edu.gatech.oad.cyberdoc.view.container.AdminPanel;
import edu.gatech.oad.cyberdoc.view.container.ContainerPanel;
import edu.gatech.oad.cyberdoc.view.container.DoctorPanel;
import edu.gatech.oad.cyberdoc.view.container.LoginPanel;
import edu.gatech.oad.cyberdoc.view.container.NursePanel;
import edu.gatech.oad.cyberdoc.view.container.PatientPanel;

public class MainFrame {

	private JFrame frmCyberdoc;
	private JPanel applicationPanel;
	private static MainFrame instance;

	/**
	 * This class's logger.
	 */
	private static final Logger LOG;

	private ContainerPanel currentSuperPanel;

	private Map<String, ContainerPanel> cards;

	static {
		LOG = Logger.getLogger("edu.gatech.oad.cyberdoc.view.MainFrame");
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame window = new MainFrame();
					window.frmCyberdoc.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	private MainFrame() {
		cards = new HashMap<String, ContainerPanel>();

		initialize();

		instance = this;
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmCyberdoc = new JFrame();
		frmCyberdoc.setTitle("Cyberdoc");
		frmCyberdoc.setBounds(100, 100, 620, 420);
		frmCyberdoc.setSize(815, 450);
		frmCyberdoc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		applicationPanel = new JPanel();
		frmCyberdoc.getContentPane().add(applicationPanel, BorderLayout.CENTER);
		applicationPanel.setLayout(new CardLayout(0, 0));

		ContainerPanel login = new LoginPanel();
		applicationPanel.add(login, "login");
		cards.put("login", login);

		ContainerPanel patientInfo = new PatientPanel();// PatientInfoPanel();
		applicationPanel.add(patientInfo, "options_patient");
		cards.put("options_patient", patientInfo);

		ContainerPanel nursePanel = new NursePanel();
		applicationPanel.add(nursePanel, "options_staff");
		cards.put("options_staff", nursePanel);

		ContainerPanel doctorPanel = new DoctorPanel();
		applicationPanel.add(doctorPanel, "options_doctor");
		cards.put("options_doctor", doctorPanel);

		ContainerPanel adminPanel = new AdminPanel();
		applicationPanel.add(adminPanel, "options_admin");
		cards.put("options_admin", adminPanel);

	}

	public void show(String panel) {
		show(panel, null);
	}

	public void show(String panel, JSONObject response) {
		if (panel.equals("login") || panel.indexOf("options") == 0) {
			LOG.info("Showing panel " + panel);

			currentSuperPanel = cards.get(panel);

			final CardLayout layout = (CardLayout) applicationPanel.getLayout();
			layout.show(applicationPanel, panel);

			if (!cards.containsKey(panel)) {
				LOG.severe("Panel \"" + panel + "\" does not exit.");
			} else if (response == null) {
				LOG.info("Response is null");
			} else if (currentSuperPanel != null) {
				currentSuperPanel.show(response);
			}
		} else {
			LOG.info("Punting panel " + panel + " to " + currentSuperPanel);
			currentSuperPanel.show(panel, response);
		}
	}

	public static MainFrame getInstance() {
		if (instance == null) {
			instance = new MainFrame();
		}

		return instance;
	}
}
